// mini-serialization.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
#include <string.h>
#include "mini_sr/InputStream.hpp"
#include "mini_sr/OutputStream.hpp"

using namespace mini_sr;
class BoxInfo
{
public:
    virtual ~BoxInfo()
    {
    }
    char _char;
    unsigned char unsigned_char;
    int _int;
    unsigned int unsigned_int;
    short _short;
    unsigned short unsigned_short;
    long _long;
    unsigned long unsigned_long;
    long long long_long;
    unsigned long long unsigned_long_long;
    float _float;
    double _double;
    bool _bool;
    
    SERIALIZATION(0)
    {
        cout<<"BoxInfo"<<endl;
        s & _char;
        s & unsigned_char;
        s & _int;
        s & unsigned_int;
        s & _short;
        s & unsigned_short;
        s & _long;
        s & unsigned_long;
        s & long_long;
        s & unsigned_long_long;
        s & _float;
        if(version > 1)
        {
            s & _double;
        }
        if(version > 0)
        {
            s & _bool;
        }
    }
    static int a;
};
    int BoxInfo::a = 3;

class Carton:public BoxInfo
{
    int with;
    string name;
    vector<string> m_boxlist;
    map<string,string> m_testmap;
    stdext::hash_map<string,string> m_testhashmap;
public:
    Carton()
    :with(-2)
    {
        for(int i = 0;i<100;i++)
        {
            name.push_back(0);
        }
        for(int i = 0;i<100;i++)
        {
            name.push_back('\\');
        }
        m_boxlist.push_back("hello");
        m_boxlist.push_back("chinese");
        m_boxlist.push_back("people");
        m_testmap["1"] = "hello";
        m_testmap["2"] = "chinese";
        m_testmap["3"] = "people";
        m_testhashmap["1"] = "hello";
        m_testhashmap["2"] = "chinese";
        m_testhashmap["3"] = "people";
    }
    SERIALIZATION(2)
    {
        s & (BoxInfo&)*this;
        cout<<"Carton"<<endl;
        s & with;
        if(version > 0)
        {
            s & name;
            s & m_boxlist;
            s & m_testmap;
        }
        if(version > 1)
        {
            s & m_testhashmap;
        }
    }
};

void test_mini_sr()
{
    Carton box,box1;
    box._char = 'c';
    box.unsigned_char = -127;
    box._int = 55;
    box.unsigned_int = 55;
    box._short = 55;
    box.unsigned_short = 55;
    box._long = 55;
    box.unsigned_long = 55;
    box.long_long = 55;
    box.unsigned_long_long = -55;
    box._float = 55.55;
    box._double = 55.55;
    box._bool = false;
    
    mini_sr::OutputStream os;
    os & box;
    string encode = os.encode();
    mini_sr::InputStream is;
    string dv = is.decode(encode);
    if(dv != os.buf())
    {
        ASSERT(FALSE);
    }
    //use hex coder
    encode = os.toHex();
    dv = is.fromHex(encode);
    if(dv != os.buf())
    {
        ASSERT(FALSE);
    }
    mini_sr::InputStream is2(os.buf());
    is & box1;
    is2 & box1;
}
#ifdef _WIN32_WCE 
int wmain()
#else
int main()
#endif 
{
    test_mini_sr();
	cout<<"end main"<<endl;
	return 0;
}
